package com.exploadingelephants.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author Sandro De Zanet
 * 
 */
public class AnimatedMachineView extends SurfaceView implements
	SurfaceHolder.Callback {

    private final AnimatedMachineThread thread;
    private long tick;

    /**
     * @param context
     */
    public AnimatedMachineView(Context context) {
	super(context);
	// adding the callback (this) to the surface holder to intercept events
	getHolder().addCallback(this);

	thread = null;
	// new AnimatedMachineThread(getHolder(),this);

	// make the GamePanel focusable so it can handle events
	setFocusable(true);
	tick = 0;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
	return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder
     * , int, int, int)
     */
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
	    int height) {
	// do nothing
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder
     * )
     */
    public void surfaceCreated(SurfaceHolder holder) {
	thread.setRunning(true);
	thread.start();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.
     * SurfaceHolder)
     */
    public void surfaceDestroyed(SurfaceHolder holder) {
	boolean retry = true;
	while (retry) {
	    try {
		thread.join();
		retry = false;
	    } catch (InterruptedException e) {
		// try again shutting down the thread
	    }
	}

    }

    /**
     * @param canvas
     */
    public void render(Canvas canvas) {
	canvas.drawColor(Color.BLACK);
	// droid.draw(canvas);
	tick++;
    }

    /**
     * This is the game update method. It iterates through all the objects and
     * calls their update method if they have one or calls specific engine's
     * update method.
     */
    public void update() {
	// check collision with right wall if heading right
	// if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
	// && droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
	// droid.getSpeed().toggleXDirection();
	// }
	// // check collision with left wall if heading left
	// if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
	// && droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
	// droid.getSpeed().toggleXDirection();
	// }
	// // check collision with bottom wall if heading down
	// if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
	// && droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {
	// droid.getSpeed().toggleYDirection();
	// }
	// // check collision with top wall if heading up
	// if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
	// && droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {
	// droid.getSpeed().toggleYDirection();
	// }
	// // Update the lone droid
	// droid.update();
    }
}
